﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using ZLDC.Common.Estate;
using ZLDC.Common.BaseUser;

namespace ZLDC.Estate.CM
{
    public partial class CustomerChange : _PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {                
                tbUserName.Text = this.info.m_sUserName;
                tbUserDept.Text = this.info.m_sDeptName;

                AddDeptTree();
            }
        }

        /// <summary>
        /// 选择部门树形菜单
        /// </summary>
        private void AddDeptTree()
        {
            string sTopDept = this.info.m_sDeptID;//默认取表中第一行为最上层部门
            string deptid_list = CacheUtility.Cache_GetAllChildDept(this.info.m_sDeptID);

            sTopDept = ConfigurationSettings.AppSettings["SalesDepartment"].ToString().Trim();//最上层部门

            ZLDC.Topo.BLL.Estate.DeptTree zdt = new ZLDC.Topo.BLL.Estate.DeptTree(sTopDept, deptid_list);
            ltlDeptTree.Text = zdt.GetZlhqDept();
        }
       
        // 提交转介
        protected void imgBtnOK_Click(object sender, EventArgs e)
        {
            string outmsg = "转介失败";

            // 转介人
            int changeDeptId = _Convert.ToInt(this.info.m_sDeptID, 0);
            string changeDept = this.info.m_sDeptName;
            int changeUserId = _Convert.ToInt(this.info.m_sUserID, 0);
            string changeUser = this.info.m_sUserName;

            //接收人           
            string userName = string.Empty; // 姓名
            string userNo = string.Empty;   // 工号            
            _Utility.GetUserNoAndUserName(utxtsub.Value.ToString().Trim(), out userName, out userNo);
            ZLDC.Topo.Model.Estate.UserInfo item = ZLDC.Topo.BLL.Estate.UserBLL.GetUserInfoByUserNo(userNo);
          
            if (item == null || item.Usr == "")
            {
                _JavaScript.Alert(this, "接收人不存在于楼盘系统中!");
                return;
            }

            int acceptDeptId = item.Dept_id;
            string acceptDept = item.DeptName;
            int acceptUserId = item.Id;
            string acceptUser = item.Name;

            int id = 0;
            string[] IDList = _Utility.GetStringBySplit(_Request.GetString("idList"), '|');

            if (IDList != null && IDList.Length > 0)
            {
                for (int i = 0; i < IDList.Length; i++)
                {
                    id = _Convert.ToInt(IDList[i], 0);

                    int count=0;
                    ZLDC.Topo.Model.Estate.CustomerInfo info = ZLDC.Topo.BLL.Estate.CustomerBLL.p_customer_detail(id);
                    //1.同组转介无需判断是否有重复客 2.转介客转回也判断是否有重复客
                    if (info.OwnerDeptId != acceptDeptId && info.AddDeptId != acceptDeptId)
                    {
                        count = ZLDC.Topo.BLL.Estate.CustomerBLL.ChangeCustomerCount(item.Dept_id, info.CustomerTel);

                    }///同组转介判断接受人是否有相同的客源
                    else if (_Utility.GetInfoBySession().m_sDeptID == acceptDeptId.ToString()) 
                    {
                        count = ZLDC.Topo.BLL.Estate.CustomerBLL.ChangeCustomerCountByUserId(item.Id, info.CustomerTel);
                    }
                    if (count > 0)
                    {
                        _JavaScript.Alert(this, "此转介客已存在该部门，不能转介！");
                        return;
                    }
                    outmsg = ZLDC.Topo.BLL.Estate.ChangecustomerLogBLL.p_changeCustomer_log_save(0, id, changeDeptId, changeDept, changeUserId, changeUser,
                        acceptDeptId, acceptDept, acceptUserId, acceptUser, 1);
                    if (mark3.Value.Trim() != "")//如果转介备注不为空时，添加转介备注;
                    {
                        ZLDC.Topo.BLL.Estate.CustomerBLL.UpdateTranRemark(id, mark3.Value.Trim());
                    }

                    _Cache.RemoveLike(CacheConst.CACHE_CUSTOMER_FOLLOW_JSON + id);
                    _Cache.Remove(CacheConst.CACHE_CUSTOMER_DETAIL_JSON + id.ToString());
                    _Cache.Remove(CacheConst.CACHE_CUSTOMER_DETAIL_INFO + id.ToString());
                }
            }

            _JavaScript.Alert(this.Page, outmsg);
            Response.Write("<script>window.close();</script>");
        }

        
    }
}
